﻿@using CleverCMS.Cms.Data.Entities
@model List<CmsMenuItem>

@functions {
    private string _str = "";
    public string CreateNode(CmsMenuItem node)    
    {
        var selNodes = Model.Where(x => x.ParentId == node.Id).ToList();
        if (!selNodes.Any())
        {
            if (User.Identity.IsAuthenticated && User.IsInRole("admin"))
                return _str += "<ul><li style='text-align: center;'><a class='add-node' href='#addPageModal' role='button' data-toggle='modal' >+</a></li></ul>";
            else return _str += "";
        }

        _str += "<ul>";

        foreach (var selNode in selNodes)
        {
            var childNode = new CmsMenuItem { Id = selNode.Id };
            _str += "<li><a href='" + (selNode.EntryId == null ? selNode.NavigateUrl : Url.Action("Content", "Home" , new { area = "", name = selNode.Entry.Alias })) + "' rel='" + selNode.Id + "'>" + selNode.Title + "</a>";
            CreateNode(childNode);
            _str += "</li>";
        }

        if (User.Identity.IsAuthenticated && User.IsInRole("admin"))
            _str += "<li style='text-align: center;'><a class='add-node' href='#addPageModal' role='button' data-toggle='modal'>+</a></li>";
        
        _str += "</ul>";
        return _str;
    }
}

<ul id="menu">
    @if (!Model.Any())
    {
        <li><a href="#">Have no CmsMenuItem</a></li>
    }
    else
    {
        foreach (var cmsMenuItem in Model)
        {
            if (cmsMenuItem.ParentId == null)
            {
                <li>
                    <a href="@Html.Raw(cmsMenuItem.EntryId == null ? cmsMenuItem.NavigateUrl: @Url.Action("Content" , "Home" , new { area = "", name = cmsMenuItem.Entry.Alias}))" rel="@cmsMenuItem.Id">
                        @cmsMenuItem.Title
                        @Html.Raw(cmsMenuItem.MenuItems.Any() ? "<span class='arrow'></span>" : "")
                    </a>
                    @Html.Raw(CreateNode(new CmsMenuItem { Id = cmsMenuItem.Id }))
                </li>
        
                _str = "";
            }
        }
    }

    @if (User.Identity.IsAuthenticated && User.IsInRole("admin"))
    {
        <li><a class="add-root" href="#addPageModal" role="button" data-toggle="modal" >+</a></li>
    }
</ul>

<div id="addPageModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true" style="display: none; width: 700px">
    <div class="modal-header">
        <button type="button" class="close close-popup" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel4">Add new page</h3>
    </div>
    <div class="modal-body">
        <div class="row" style="margin-left: 0px;">
            <div class="span2"><p>Title</p></div> 
            <div class="span10">
                <input type="text" name="title" style="width: 50%"/>
            </div>        
        </div>
        <p>Content</p>
        <textarea name="pageContent" id="pageContent" style="width: 100%; height: 200px;"></textarea>
        @if (WebSecurity.IsAuthenticated)
        {
            <script type="text/javascript">
                CKEDITOR.replace('pageContent',
                    {
                        scayt_autoStartup: false,
                        filebrowserBrowseUrl: "@Url.Content("~/Scripts/ckfinder/ckfinder.html")",
                        filebrowserImageBrowseUrl: "@Url.Content("~/Scripts/ckfinder/ckfinder.html?type=Images")",
                        filebrowserFlashBrowseUrl: "@Url.Content("~/Scripts/ckfinder/ckfinder.html?type=Flash")",
                        filebrowserUploadUrl: "@Url.Content("~/Scripts/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files")",
                        filebrowserImageUploadUrl: "@Url.Content("~/Scripts/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images")",
                        filebrowserFlashUploadUrl: "@Url.Content("~/Scripts/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash")",
                        filebrowserWindowWidth: "1000",
                        filebrowserWindowHeight: "700"
                    });
            </script> 
        }

    </div>
    <div class="modal-footer">
        <button class="btn close-popup" data-dismiss="modal" aria-hidden="true">Close</button>
        <button data-dismiss="modal" class="btn btn-success savePage">Add</button>
    </div>
</div>

<script type="text/javascript">
    // MenuItem id
    var id;

    $(document).ready(function() {
        // Input to elem with id...
        
        $(".add-node").on("click", function () {
            id = $(this).parent().parent().siblings("a").attr("rel");
        });
        $(".add-root").on("click", function () {
            id = null;
        });
        // Save page and menuItem
        $(".savePage").on("click", function () {
            var dataObject = JSON.stringify({
                'menuParentId': id,
                'menuTitle': $("input[name='title']").val(),
                'pageContent': CKEDITOR.instances.pageContent.getData()
            });
            $.ajax({
                type: "POST",
                contentType: 'application/json',
                url: "@Url.Action("AddMenuItem","Navigation")",
                data: dataObject,
                success: function (msg) {
                    if (msg.success == true) {
                        if (id == null) {
                            $("ul#menu > li:last-child").before("<li><a href='" + msg.pageLink + "' rel='" + msg.menuId + "'>" + $("input[name='title']").val() + "</a><ul><li style='text-align: center;'><a class='add-node' onclick='addNode(this)' href='#addPageModal' role='button' data-toggle='modal' >+</a></li></ul></li>");
                        } else {
                            var parentUlObj = $("a[rel='" + id + "']").siblings("ul");
                            parentUlObj.find("li:last").before("<li><a href='" + msg.pageLink + "' rel='" + msg.menuId + "'>" + $("input[name='title']").val() + "</a><ul><li style='text-align: center;'><a class='add-node' onclick='addNode(this)' href='#addPageModal' role='button' data-toggle='modal' >+</a></li></ul></li>");
                        }
                    }
                    //Clear fields
                    $("input[name='title']").val("");
                    CKEDITOR.instances.pageContent.setData('');
                }
            });
        });
        //Close popup clear
        $(".close-popup").on("click", function() {
            $("input[name='title']").val("");
            CKEDITOR.instances.pageContent.setData('');
        });
    });
    
    // To dynamic element
    function addNode(elem) {
        id = $(elem).parent().parent().siblings("a").attr("rel");
    }
</script>

